/* ***********************************************************
* A short word on how to use this automagically generated file.
* We're often asked in the ignite gitter channel how to connect
* to a to a third party api, so we thought we'd demonstrate - but
* you should know you can use sagas for other flow control too.
*
* Other points:
*  - You'll need to add this saga to sagas/index.js
*  - This template uses the api declared in sagas/index.js, so
*    you'll need to define a constant in that file.
*************************************************************/

import { call, put } from 'redux-saga/effects'
import LoginActions from '../Redux/LoginRedux'
import UserActions from '../Redux/UserRedux'

export function * getLogin (api, action) {
  const { mobile, password, deviceId } = action
  // make the call to the api
  const response = yield call(api.login, mobile, password, deviceId)

  // success?
  if (response.ok) {
    if (response.data.code == 1) {
      const token = response.data.data.token
      yield put(LoginActions.loginSuccess(token))
      yield put(UserActions.userSuccess(response.data.data))
    } else {
      yield put(LoginActions.loginFailure())
    }
  } else {
    yield put(LoginActions.loginFailure())
  }
}
